package kr.ac.kaist.pointdaproject;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;

public class LocationInformation {

	LocationManager mlocManager;
	LocationListener mlocListener;
	String provider;
	Location location;
	String bestProvider;
	Criteria criteria;

	public LocationInformation(){
		super();
	}
	
	public LocationInformation(LocationManager mlocManager){

		this.mlocManager = mlocManager;

		mlocListener = new LocationListener(){
			public void onLocationChanged(Location loc) {
				loc.getLatitude(); 
				loc.getLongitude();
				Log.i("debug",loc.getProvider());
			} 

			public void onProviderDisabled(String provider){}
			public void onProviderEnabled(String provider) {}  
			public void onStatusChanged(String provider, int status, Bundle extras){} 

		}; 

		criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(false);
		criteria.setCostAllowed(true);

		bestProvider = mlocManager.getBestProvider(criteria, true);

		location = mlocManager.getLastKnownLocation(bestProvider);

	} 

	public double getLatitude(){
		bestProvider = mlocManager.getBestProvider(criteria, true);
		mlocManager.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
		location = mlocManager.getLastKnownLocation(bestProvider);

		if(location != null)
			return location.getLatitude();
		else
			return 0;

	} 

	public double getLongitude(){

		bestProvider = mlocManager.getBestProvider(criteria, true);
		mlocManager.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
		location = mlocManager.getLastKnownLocation(bestProvider);

		if(location != null)
			return location.getLongitude();
		else
			return 0;

	}
	
	public double getAltitude(){

		bestProvider = mlocManager.getBestProvider(criteria, true);
		mlocManager.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
		location = mlocManager.getLastKnownLocation(bestProvider);

		if(location != null)
			return location.getAltitude();
		else 
			return 0;

	}
}

